package com.atguigu.write;

import com.atguigu.bean.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;

import java.io.IOException;
import java.util.HashMap;

public class Es01_SingleWrite {
    public static void main(String[] args) throws IOException {
        //1.创建客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //3.获取客户端连接
        JestClient jestClient = jestClientFactory.getObject();

        HashMap<String, String> data = new HashMap<>();
        data.put("id", "104");
        data.put("name", "搜查官");

        //4.写入数据
        //JSON字符串
//        Index index = new Index.Builder("{\n" +
//                "  \"id\":\"103\",\n" +
//                "  \"name\":\"教父\"\n" +
//                "}\n")
//                .id("1003")
//                .index("movie_chn_2022")
//                .type("movie")
//                .build();

        //Map集合
//        Index index = new Index.Builder(data)
//                .index("movie_chn_2022")
//                .type("movie")
//                .id("1004")
//                .build();

        Movie movie = new Movie("105", "桃花侠大战菊花怪");

        // JavaBean
        Index index = new Index.Builder(movie)
                .index("movie_chn_2022")
                .type("movie")
                .id("1005")
                .build();


        jestClient.execute(index);



        //关闭连接
        jestClient.shutdownClient();

    }
}
